import numpy as np

np.random.seed(1)
X = np.random.randint(1,10, size=30)
print(X)

'''
1.请将X处理为一个3列的矩阵
'''
print("="*10,"打印一个3列的矩阵","="*10)
new = X.reshape(-1,3)
print(new)

print("="*30)

'''
2.将第三列中，小于等于3的修改为0、大于3且小于等于6的修改为1、大于6的修改为2，结果 如下: 
'''
print("="*10,"修改第三列为0，1，2","="*10)

new_arr = np.hsplit(new,3)[2]
new_arr[new_arr <=3] = 0
bool_index = (new_arr <= 6) & (new_arr > 3)
new_arr[bool_index] = 1
new_arr[new_arr > 6] = 2
print(new)
print("="*30)

print("="*10,'3 分离样本','='*10)

'''
请分离出样本 的特征和分类 标记，分别存放在两个变量中，用 X_train 存放样本特征(红色部份), y_train 存放分类标记(绿色部份)
'''
split_array = np.hsplit(new,3)
x_train = np.hstack((split_array[0],split_array[1]))
y_train = split_array[2]
print(x_train, '\n===== 第三列 =====\n',y_train)

print('='*10,'4.分类','='*20)
'''
请用 numpy 的比较运算，通过 y_train 中的数据，分离出 X_train 中的 3 个分类
'''

bool_0_index = y_train == 0
bool_1_index = y_train == 1
bool_2_index = y_train == 2
print('='*10,"分类为 0 ",'='*10)
print(x_train[bool_0_index.flatten()])

print('='*10,"分类为 1 ",'='*10)
print(x_train[bool_1_index.flatten()])

print('='*10,"分类为 2 ",'='*10)
print(x_train[bool_2_index.flatten()])



